﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;
using System.Text.RegularExpressions;
using System.Data;

namespace NLLSite.UserControl
{
    public partial class Search : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string show = "";
            DAL_Class.BaiViet objBaiViet = new BaiViet();

            string keyword = Request.QueryString["keyword"];
            
            IEnumerable<view_UI_ALLArticle> result = objBaiViet.Search(keyword);
            try 
	        {
                foreach (view_UI_ALLArticle r in result)
                {
                    show += "<div class=\"box-center-item-tin\" style=\"margin-left: 6px !important;\">"
                                        + "<a href=\"/bai-viet/" + r.ID + "/" + ConvertToUnSign(r.Title) + "\" style=\"float: left; margin-right: 10px; margin-bottom: 10px;\">"
                                        + "<img height=\"100px\" width=\"130px\" style=\"border: 1px solid rgb(204, 204, 204); padding: 1px;\" src=\"" + r.ThumbImage + "\"  >"
                                                + "</a><a href=\"/bai-viet/" + r.ID + "/" + ConvertToUnSign(r.Title) + "\"><b>" + r.Title + "</b></a>"
                                               + " <p style=\"text-align: justify;\">" + r.Description + "</p>"
                                       + " </div>"
        + "<div class=\"clr\"></div>";
                }
	        }
	        catch (Exception)
	        {
                show = "Không tìm thấy kết quả nào.";
		        //throw;
	        }
                
            
            lblResult.Text = show;
        }
        public static string ConvertToUnSign(string text)
        {

            for (int i = 32; i < 48; i++)
            {

                text = text.Replace(((char)i).ToString(), " ");

            }

            text = text.Replace(".", "-");

            text = text.Replace(" ", "-");

            text = text.Replace(",", "-");

            text = text.Replace(";", "-");

            text = text.Replace(":", "-");



            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");



            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);

            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').ToLower();

        }
    }
}